package com.delivery.utils;

import java.io.ByteArrayOutputStream;
import java.util.regex.Pattern;

import android.content.Context;
import android.widget.Toast;

/**
 * 
 * @author LY
 * 
 */
public class StringUtils {
	
	private static Toast toast = null;
	

	// 判断输入的字符串是否是数�?
	public static boolean isNum(String str) {
		try {
			Long.valueOf(str);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	
	public static String replacAllToEntity(String msgBody){
		String msg = msgBody;
		if(msgBody != "" && msgBody != null){
			msg = msgBody.replaceAll("'", "&apos;");
			msg = msgBody.replaceAll("\"", "&quot;");
		}
		return msg;
	}

	
	/**
	 * 替换所以特殊字符串
	 * 
	 * @param content
	 * @param type
	 * @return
	 */
	public static String replaceAllChar(String content, String type) {
		String src = new String();
		if (isEmpty(content)) {
			return content;
		}
		if (isEmpty(type)) {
			type = "other";
		}
		src = replaceAll("\\?", "？", content);
		src = replaceAll("\"", "“", src);
		src = replaceAll("'", "‘", src);
		src = replaceAll("%", "％", src);
		src = replaceAll("&", "＆", src);
		src = replaceAll("\\\\", "*fxg;", src);
		src = replaceAll("/", "*zxg;", src);
		if (type == "chat") {
			src = replaceAll("<", "&lt;", src);
			src = replaceAll(">", "&gt;", src);
		} else if (type == "other") {
			src = replaceAll("\r", "*lr;", src);
			src = replaceAll("\n", "*ln;", src);
			src = replaceAll("<", "*lt;", src);
			src = replaceAll(">", "*gt;", src);
		} else if (type == "html") {
			src = replaceAll("<", "&lt;", src);
			src = replaceAll(">", "&gt;", src);
		}
		return src;
	}

	/**
	 * 反替换特殊字符串
	 * 
	 * @param content
	 * @param type
	 * @return
	 */

	public static String antiReplaceAllChar(String content, String type) {
		String src = new String();
		if (isEmpty(content)) {
			return content;
		}
		if (isEmpty(type)) {
			type = "other";
		}
		src = replaceAll("？", "?", content);
		src = replaceAll("“", "\"", src);
		src = replaceAll("‘", "'", src);
		src = replaceAll("％", "%", src);
		src = replaceAll("＆", "&", src);
		src = replaceAll("\\*fxg;", "\\\\", src);
		src = replaceAll("\\*zxg;", "/", src);

		if (type == "chat") {

		} else if (type == "other") {
			src = replaceAll("\\*lr;", "\r", src);
			src = replaceAll("\\*ln;", "\n", src);
			src = replaceAll("\\*lt;", "<", src);
			src = replaceAll("\\*gt;", ">", src);
		} else if (type == "nothtml") {
			src = replaceAll("&lt;", "<", src);
			src = replaceAll("&gt;", ">", src);
		} else if (type == "html") {

		}

		return src;
	}
	
	public static String replaceAll(String st1, String st2, String content) {

		return Pattern.compile(st1).matcher(content).replaceAll(st2);
	}
	
	public static String replaceAllBackslash(String content){
		String src = new String();
		if (isEmpty(content)) {
			return content;
		}
		src = replaceAll("\\\\", "/", content);
		return src;
	}
	
	public static boolean isEmpty(String str) {
		return str == null || str.length()<=0 || str.equals("") || str.equals("null");
	}
	
	public static boolean isNotEmpty(String str) {
		return null != str && !"".equals(str) && !"null".equals(str);
	}
	
	/**
	 * 根据字符串截取
	 * @param str
	 * @param type
	 * @return
	 */
	public static String subStrIndexOf(String str,String type){
		if(StringUtils.isEmpty(str)){
			return "";
		}
		return str.substring(str.lastIndexOf(type)+1, str.length());
	}
	
	/**
	 * 根据长度截取
	 * @param str
	 * @param len
	 * @return
	 */
	public static String subStrLen(String str,int len){
		if(StringUtils.isEmpty(str)){
			return "";
		}
		if(str.length() <= len){
			return str;
		}
		return str.substring(0, len);
	}
	
	
	/**
	 * 给�?添加单引号，方便sql语句in查询
	 * @param st
	 * @param type
	 * @return
	 */
	public static String addSingleQuotes(String st,String type){
		if(isEmpty(st) || isEmpty(type)){
			return "'0'";
		}
		String[] array = st.split(type);
		StringBuffer sb = new StringBuffer();
		for(int i=0,l=array.length;i<l;i++){
			sb.append("'"+array[i]+"',");
		}
		if(sb.toString().endsWith(",")){
			sb.delete(sb.length()-1, sb.length());
		}
		return sb.toString();
	}
	
	/**
	 * 根据类型分割字符�?
	 * @param st
	 * @param type
	 * @return
	 */
	public static String[] spliteStr(String st,String type){
		if(isEmpty(st) || isEmpty(type)){
			return new String[0];
		}
		return st.split(type);
	}
	
	public static void ToskShow(Context context,String msg){
		if(isEmpty(msg)){
			return;
		}
		if(toast == null){
			toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
		}
		toast.setText(msg);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();
		
	}
	
	// 转化字符串为十六进制编码 
	public static String toHexString(String s) 
	{ 
		String str=""; 
		for (int i=0;i<s.length();i++) 
		{ 
			int ch = (int)s.charAt(i); 
			String s4 = Integer.toHexString(ch); 
			str = str + s4; 
		} 
		return str; 
	} 
	
	// 转化十六进制编码为字符串 
	public static String toStringHex(String s) 
	{ 
		byte[] baKeyword = new byte[s.length()/2]; 
		try 
		{ 
			for(int i = 0; i < baKeyword.length; i++) 
			{ 
				baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16)); 
			
				s = new String(baKeyword, "utf-8");//UTF-16le:Not 
			}
		} 
		catch(Exception e) 
		{ 
			e.printStackTrace(); 
		} 
		return s; 
	} 
	/* 
	* 16进制数字字符集 
	*/ 
	private static String hexString="0123456789ABCDEF"; 
	/* 
	* 将字符串编码成16进制数字,适用于所有字符（包括中文） 
	*/ 
	public static String encode(String str) 
	{ 
		//根据默认编码获取字节数组 
		byte[] bytes=str.getBytes(); 
		StringBuilder sb=new StringBuilder(bytes.length*2); 
		//将字节数组中每个字节拆解成2位16进制整数 
		for(int i=0;i<bytes.length;i++) 
		{ 
			sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); 
			sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); 
		} 
		return sb.toString(); 
	} 
	/* 
	* 将16进制数字解码成字符串,适用于所有字符（包括中文） 
	*/ 
	public static String decode(String bytes) 
	{ 
		ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); 
		//将每2位16进制整数组装成一个字节 
		for(int i=0;i<bytes.length();i+=2) 
		baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)))); 
		return new String(baos.toByteArray()); 
	} 

	
	public static void main(String[] args) {
		//System.out.println(dealOfflineMessage("1|2|3|5|5|", "1111"));
		//System.out.println(replaceAllChar("2323*zxg;*zxg;&lt;“我的电话：�?lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;&gt;&gt;$$^％＆*#"));
		//System.out.println(replaceAllChar("eeeeeeee*zxg;"));
		//System.out.println(addSingleQuotes("99990065,99990063,", ""));
		//System.out.println(encode("中文")); 
		//System.out.println(decode(encode("中文"))); 
		
	}
}
